package org.xy.processor;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.xy.bean.School;
import org.xy.bean.Student;
import org.xy.dao.BasicDAO;
import org.xy.dao.SchoolDAO;
import org.xy.dao.StudentDAO;

@Component("annotaionTX")
public class AopTX {
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	protected BasicDAO basicDAO;

	@Resource
	protected StudentDAO studentDAO;

	@Resource
	protected SchoolDAO schoolDAO;

	public void insertSchReqTX() {
		this.logger.info("School insert use required tx");
		schoolDAO.insert(new School(100, "Sch-Alone", "OP"));
	}

	public void insertStdNewReqTX() {
		this.logger.info("Student insert use new required tx");
		studentDAO.insert(new Student(100, "Std-Alone", 11, new School(100,
				"Sch-Alone", "OP")));
	}

	public void insertReqTX(boolean abortTX) {
		this.logger.info("School and student insert use required tx");
		schoolDAO.insert(new School(1, "Sch-A", "OP"));
		if (abortTX) {
			logger.info("Abort the student insert");
			throw new RuntimeException("Abort the student insert");
		}
		studentDAO.insert(new Student(1, "Std-A", 11, new School(1, "Sch-A",
				"OP")));
	}

	public void insertComplexReqTX(boolean abortTX) {
		this.logger.info("School and student insert use required tx");
		schoolDAO.insert(new School(1, "Sch-A", "OP"));

		this.insertNeverTX(abortTX);
		if (abortTX) {
			logger.info("Abort the student insert");
			throw new RuntimeException("Abort the student insert");
		}
		studentDAO.insert(new Student(1, "Std-A", 11, new School(1, "Sch-A",
				"OP")));
	}

	public void insertNeverTX(boolean abortTX) {
		this.logger.info("School and student insert use support tx");
		schoolDAO.insert(new School(2, "Sch-B", "OP"));
		if (abortTX) {
			logger.info("Abort the student insert");
			throw new RuntimeException("Abort the student insert");
		}
		studentDAO.insert(new Student(2, "Std-B", 11, new School(2, "Sch-B",
				"OP")));
	}

	public void insertMandatoryTX(boolean abortTX) {
		this.logger.info("School and student insert use support tx");
		schoolDAO.insert(new School(2, "Sch-B", "OP"));
		if (abortTX) {
			logger.info("Abort the student insert");
			throw new RuntimeException("Abort the student insert");
		}
		studentDAO.insert(new Student(2, "Std-B", 11, new School(2, "Sch-B",
				"OP")));
	}
}
